import SwiftUI
import NetworkExtension

struct SplashView: View {
    @State private var progress: CGFloat = 0
    @State private var isCompleted: Bool = false
    @StateObject private var speedViewModel = SpeedViewModel()
    @StateObject private var timerViewModel = TimerViewModel()
    
    @Binding var vpnStatus: Bool
    var body: some View {
        NavigationView {
            ZStack {
                Rectangle()
                    .foregroundColor(Color("startBack"))
                    .edgesIgnoringSafeArea(.all)
                
                VStack {
                    Image("line")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(maxWidth: .infinity)
                    
                    Spacer()
                }
                
                VStack {
                    Spacer()
                    // 应用图标
                    Image("icon")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 200, height: 200)
                        .cornerRadius(10.0)
                    
                    Spacer()
                    
                    ZStack {
                        // 进度条背景
                      
                        
                        LinearProgress(
                            percentage: progress / 100,
                            backgroundColor: .gray,
                            foregroundColor: LinearGradient(gradient: Gradient(colors: [Color("alphaStart"), Color("alphaStart")]), startPoint: .leading, endPoint: .trailing)
                        )
                        .padding(.horizontal,40)
                        .frame(height:10)
                        .onAppear {
                            Timer.scheduledTimer(withTimeInterval: 0.03, repeats: true) { timer in
                                withAnimation(.linear(duration: 0.03)) {
                                    progress += 1
                                }
                                if progress >= 100 {
                                    timer.invalidate()
                                    isCompleted = true
                                }
                            }
                        }
                    }
                    Spacer()
                }
                .navigationBarHidden(true)
                
                NavigationLink(destination: ContentView(vpnStatus: $vpnStatus)
                    .environmentObject(speedViewModel)
                    .environmentObject(timerViewModel),
                               isActive: $isCompleted) {
                    EmptyView()
                }
                .hidden()
            }
    
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarHidden(true)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}


